package com.bilibili.controller.resolver.param;

import com.bilibili.annocation.CurUser;
import com.bilibili.utils.WebOperations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * 用于去解析控制器方法的自定义入参规则的
 */
public class CurUserParamResolver implements HandlerMethodArgumentResolver {

    @Autowired
    private WebOperations webOperations;


    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        //当满足什么条件的时候需要来做下面的resolveArgument对应的参数解析工作
        return methodParameter.hasParameterAnnotation(CurUser.class);
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        if (methodParameter.hasParameterAnnotation(CurUser.class)) {
            return webOperations.getLoginUser();
        } else {
            return null;
        }
    }
}
